﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SIMS.School;
using SIMS.Global;
using SIMS.Company;
using SIMS.Student;
using System.Text;

public partial class School_StudentTimeTable_CreateEdit : System.Web.UI.Page
{
    private void BindRoomList()
    {
        ddlRoomList.Items.Clear();
        ddlRoomList.Items.Add(new ListItem("Select", "-1"));

        RoomList _roomList = new RoomList();
        RoomDB _roomDB = new RoomDB();
        _roomList = _roomDB.GetRoomList(Convert.ToInt32(hidCompanyId.Value));
        if (_roomList != null && _roomList.Count > 0)
        {
            foreach (Room _cd in _roomList)
            {
                ddlRoomList.Items.Add(new ListItem(_cd.Name, _cd.Id.ToString()));
            }
        }
    }

    private void BindCourseDetail()
    {
        ddlCourseDetailList.Items.Clear();
        ddlCourseDetailList.Items.Add(new ListItem("Select", "-1"));

        CourseDetailList _courseDetailList = new CourseDetailList();
        CourseDetailDB _courseDetailDB = new CourseDetailDB();
        _courseDetailList = _courseDetailDB.GetCourseDetailList(Convert.ToInt32(hidCompanyId.Value));
        if (_courseDetailList != null && _courseDetailList.Count > 0)
        {
            foreach (CourseDetail _cd in _courseDetailList)
            {
                ddlCourseDetailList.Items.Add(new ListItem(_cd.Name, _cd.Id.ToString()));
            }
        }
    }

    private void ShowCourseDetail()
    {
        CourseDetail _courseDetail = new CourseDetail();
        CourseDetailDB _courseDetailDB = new CourseDetailDB();
        _courseDetail = _courseDetailDB.GetCourseDetail(Convert.ToInt32(ddlCourseDetailList.Text));

        if (_courseDetail != null && _courseDetail.Id > 0)
        {
            StringBuilder _strCourseDetail = new StringBuilder();
            _strCourseDetail.Append("<table cellpadding='3' cellspacing='4'>");
            _strCourseDetail.Append("<tr><td>Course:</td><td>" + _courseDetail.CourseName + "</td></tr>");
            _strCourseDetail.Append("<tr><td>Standard Level:</td><td>" + _courseDetail.StandardLevelName + "</td></tr>");
            _strCourseDetail.Append("<tr><td>Major Field:</td><td>" + _courseDetail.MajorFieldName + "</td></tr>");
            _strCourseDetail.Append("</table>");

            lblCourseDetail.Text = _strCourseDetail.ToString();
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Session[Global.CompanyId] != null)
            {
                hidCompanyId.Value = Session[Global.CompanyId].ToString();
                lblCompany.Text = Session[Global.CompanyName].ToString();
                BindCourseDetail();
                BindRoomList();
                if (Request.Params["id"] != null)
                {
                    int _StudentTimeTableId = 0;
                    if (int.TryParse(Request.Params["id"].ToString(), out _StudentTimeTableId) && _StudentTimeTableId > 0)
                    {
                        StudentTimeTable _StudentTimeTable = new StudentTimeTable();
                        StudentTimeTableDB _StudentTimeTableDB = new StudentTimeTableDB();
                        _StudentTimeTable = _StudentTimeTableDB.GetStudentTimeTable(_StudentTimeTableId);

                        if (_StudentTimeTable.Id > 0)
                        {
                            if (_StudentTimeTable.Error.ErrorNo > 0)
                            {
                                lblMessage.Text = _StudentTimeTable.Error.ErrorMessage;
                            }
                            else
                            {
                                hidStudentTimeTableId.Value = _StudentTimeTableId.ToString();
                                txtStudentTimeTableName.Text = _StudentTimeTable.Name;
                                txtDescription.Text = _StudentTimeTable.Description;
                                ddlCourseDetailList.Text = _StudentTimeTable.CourseDetailID.ToString();
                                ddlRoomList.Text = _StudentTimeTable.RoomID.ToString();
                            }
                        }
                    }
                    else
                    {
                        lblMessage.Text = "Invalid parameter!";
                    }
                }
            }
            else
            {
                lblMessage.Text = "No company found!";
                btnSubmit.Enabled = false;
            }
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            if (Page.IsValid)
            {
                StudentTimeTable StudentTimeTable = new StudentTimeTable();
                StudentTimeTableDB _StudentTimeTableDB = new StudentTimeTableDB();
                int _StudentTimeTableId = 0;

                if (int.TryParse(hidStudentTimeTableId.Value, out _StudentTimeTableId) && _StudentTimeTableId > 0)
                {
                    StudentTimeTable = _StudentTimeTableDB.GetStudentTimeTable(_StudentTimeTableId);
                }
                else
                {
                    StudentTimeTable.Id = 0;
                    StudentTimeTable.Error = new MyError();
                }

                StudentTimeTable.Name = txtStudentTimeTableName.Text;

                if (txtDescription.Text.Trim().Length > 0)
                {
                    StudentTimeTable.Description = txtDescription.Text;
                }
                else
                {
                    StudentTimeTable.Description = string.Empty;
                }

                StudentTimeTable.CourseDetailID = Convert.ToInt32(ddlCourseDetailList.Text);
                StudentTimeTable.RoomID = Convert.ToInt32(ddlRoomList.Text);

                StudentTimeTable.IsActive = false;

                _StudentTimeTableDB.Save(StudentTimeTable);

                if (StudentTimeTable.Error.ErrorNo > 0)
                {
                    if (StudentTimeTable.Error.ErrorNo == 1)
                    {
                        lblMessage.Text = StudentTimeTable.Error.ErrorMessage;
                    }
                    else
                    {
                        lblMessage.Text = "(" + StudentTimeTable.Error.ErrorLocation + ")";
                        lblMessage.Text += StudentTimeTable.Error.ErrorMessage;
                        lblMessage.Text += "<br/><br/>" + StudentTimeTable.Error.ErrorStack;
                    }
                }
                else
                {
                    lblMessage.Text = "Saved successfully!";
                    Response.Redirect("default.aspx?companyid=" + hidCompanyId.Value);
                }
            }
            else
            {
                lblMessage.Text = "Invalid form.";
            }
        }
        catch (Exception Ex)
        {
            lblMessage.Text = Ex.Message;
            MyError.CreateExceptionLog(Ex, "Company_CreateEdit");
        }

    }

    protected void ddlCourseDetailList_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlCourseDetailList.Items.Count > 0 && ddlCourseDetailList.SelectedIndex > 0)
        {
            ShowCourseDetail();
        }
        else
        {
            lblCourseDetail.Text = "Please select course detail.";
        }
    }

}